package org.bgu.survey;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Admin
 */
public class Admin extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Admin() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Connection connection = null;
		ResultSet resultset = null;
		Statement statement = null;
		try {
			String userName = "root";
			String password = "12345678";
			String url = "jdbc:mysql://127.0.0.1:3306/teza"; // Vista version
			Class.forName("com.mysql.jdbc.Driver");

			connection = DriverManager.getConnection(url, userName, password);

			statement = connection.createStatement();
//			resultset = statement.executeQuery("select * from vehicle");
			resultset = statement.executeQuery("SELECT * FROM survey;");

			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			out.println("<html>");
			out.println("<head>");
			out.println("<title>Survey</title>");
			out.println("</head>");
			out.println("<body>");
			out.println("<h1>survey table</h1>");
			out.println("<table border=\"1\" style=\"width: 100%\">");
			out.println("<caption>");
			out.println("</caption>");
			out.println("<col/><col/>");
			out.println("<tbody>");
			printHeader(out);
			while (resultset.next()) {
				printRow(out, resultset);
			}
		    out.println("</tbody>");
		    out.println("</table>");
			out.println("</body>");
			out.println("</html>");
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (statement != null) {
					statement.close();
				}
				if (connection != null) {
					connection.close();
				}
			}
			// catch (SQLException sqle) {
			// sqle.printStackTrace();
			// }
			catch (Exception e) {
				e.printStackTrace();
			}
		}
//		request.setAttribute("resultset", resultset);
//
//		getServletConfig().getServletContext().getRequestDispatcher(
//				"/master.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	
	private void printCol(PrintWriter out, int val) {
		printCol(out, (Object)val);
	}
	
	private void printCol(PrintWriter out, boolean val) {
		printCol(out, (Object)val);
	}
	
	private void printCol(PrintWriter out, Object val) {
		out.println("<td>");
		out.println(val);
		out.println("</td>");
	}
		
	private void printHeader(PrintWriter out) {
		out.println("<tr>");
		printCol(out, Survey.TARGET);
		printCol(out, Survey.HOME_TO_WORK_NOW);
		printCol(out, Survey.HOME_TO_WORK_5);
		printCol(out, Survey.HOME_TO_WORK_10);
		printCol(out, Survey.VEHICLE_NOW);
		printCol(out, Survey.VEHICLE_5);
		printCol(out, Survey.VEHICLE_10);
		printCol(out, Survey.SENIORITY);
		printCol(out, Survey.WORK_ADVANCE_METER);
		printCol(out, Survey.MOVED_FLAT);
		printCol(out, Survey.WORK_CAUSED_MOVE_METER);
		printCol(out, Survey.TIME_CHANGED_AFTER_MOVE);
		printCol(out, Survey.SERCHED_FLAT_CLOSER);
		printCol(out, Survey.HAD_OPPORTUNITY_MOVE_CLOSER);
		printCol(out, Survey.PLAN_TO_MOVE);
		printCol(out, Survey.CONSIDERED_WORK_CLOSER_TO_HOME_METER);
		printCol(out, Survey.YOU_EARN_MONEY);
		printCol(out, Survey.AGE);
		printCol(out, Survey.SEX);
		printCol(out, Survey.SPOUSE_WORK_CITY);
		printCol(out, Survey.MARITAL_STATUS);
		printCol(out, Survey.CHILDREN);
		printCol(out, Survey.SALARY);
		printCol(out, Survey.SCHOOL_YEARS);
		printCol(out, Survey.FLAT_NOW);
		printCol(out, Survey.FLAT_5);
		printCol(out, Survey.FLAT_10);
		printCol(out, Survey.MISHLAH_YAD);
		printCol(out, Survey.TEL);
        out.println("</tr>");
	}
	
	private void printRow(PrintWriter out, ResultSet rs) throws SQLException {
		out.println("<tr>");
		printCol(out, rs.getInt(Survey.HOME_TO_WORK_NOW));
		printCol(out, rs.getInt(Survey.HOME_TO_WORK_5));
		printCol(out, rs.getInt(Survey.HOME_TO_WORK_10));
		printCol(out, rs.getInt(Survey.VEHICLE_NOW));
		printCol(out, rs.getInt(Survey.VEHICLE_5));
		printCol(out, rs.getInt(Survey.VEHICLE_10));
		printCol(out, rs.getInt(Survey.SENIORITY));
		printCol(out, rs.getInt(Survey.WORK_ADVANCE_METER));
		printCol(out, rs.getBoolean(Survey.MOVED_FLAT));
		printCol(out, rs.getInt(Survey.WORK_CAUSED_MOVE_METER));
		printCol(out, rs.getInt(Survey.TIME_CHANGED_AFTER_MOVE));
		printCol(out, rs.getBoolean(Survey.SERCHED_FLAT_CLOSER));
		printCol(out, rs.getBoolean(Survey.HAD_OPPORTUNITY_MOVE_CLOSER));
		printCol(out, rs.getBoolean(Survey.PLAN_TO_MOVE));
		printCol(out, rs.getInt(Survey.CONSIDERED_WORK_CLOSER_TO_HOME_METER));
		printCol(out, rs.getBoolean(Survey.YOU_EARN_MONEY));
		printCol(out, rs.getInt(Survey.AGE));
		printCol(out, rs.getBoolean(Survey.SEX));
		printCol(out, rs.getString(Survey.SPOUSE_WORK_CITY));
		printCol(out, rs.getBoolean(Survey.MARITAL_STATUS));
		printCol(out, rs.getInt(Survey.CHILDREN));
		printCol(out, rs.getInt(Survey.SALARY));
		printCol(out, rs.getInt(Survey.SCHOOL_YEARS));
		printCol(out, rs.getInt(Survey.FLAT_NOW));
		printCol(out, rs.getInt(Survey.FLAT_5));
		printCol(out, rs.getInt(Survey.FLAT_10));
		printCol(out, rs.getInt(Survey.MISHLAH_YAD));
		printCol(out, rs.getString(Survey.TEL));
        out.println("</tr>");
	}
	
//	String welcomeMessage = HOME_TO_WORK_NOW + " = [" + home_to_work_now + "] <br/>";
//	welcomeMessage += HOME_TO_WORK_5 + " = [" + home_to_work_5 + "] <br/>";
//	welcomeMessage += HOME_TO_WORK_10 + " = [" + home_to_work_10 + "] <br/>";
//	welcomeMessage += VEHICLE_NOW + " = [" + vehicle_now + "] <br/>";
//	welcomeMessage += VEHICLE_5 + " = [" + vehicle_5 + "] <br/>";
//	welcomeMessage += VEHICLE_10 + " = [" + vehicle_10 + "] <br/>";
//	welcomeMessage += SENIORITY + " = [" + seniority + "] <br/>";
//	welcomeMessage += WORK_ADVANCE_METER + " = [" + work_advance_meter + "] <br/>";
//	welcomeMessage += MOVED_FLAT + " = [" + moved_flat + "] <br/>";
//	welcomeMessage += WORK_CAUSED_MOVE_METER + " = [" + work_caused_move_meter + "] <br/>";
//	welcomeMessage += TIME_CHANGED_AFTER_MOVE + " = [" + time_changed_after_move + "] <br/>";
//	welcomeMessage += SERCHED_FLAT_CLOSER + " = [" + serched_flat_closer + "] <br/>";
//	welcomeMessage += HAD_OPPORTUNITY_MOVE_CLOSER + " = [" + had_opportunity_move_closer + "] <br/>";
//	welcomeMessage += PLAN_TO_MOVE + " = [" + plan_to_move + "] <br/>";
//	welcomeMessage += CONSIDERED_WORK_CLOSER_TO_HOME_METER + " = [" + considered_work_closer_to_home_meter + "] <br/>";
//	welcomeMessage += YOU_EARN_MONEY + " = [" + you_earn_money + "] <br/>";
//	welcomeMessage += AGE + " = [" + age + "] <br/>";
//	welcomeMessage += SEX + " = [" + sex + "] <br/>";
//	welcomeMessage += SPOUSE_WORK_CITY + " = [" + spouse_work_city + "] <br/>";
//	welcomeMessage += MARITAL_STATUS + " = [" + marital_status + "] <br/>";
//	welcomeMessage += CHILDREN + " = [" + children + "] <br/>";
//	welcomeMessage += SALARY + " = [" + salary + "] <br/>";
//	welcomeMessage += SCHOOL_YEARS + " = [" + school_years + "] <br/>";
//	welcomeMessage += FLAT_NOW + " = [" + flat_now + "] <br/>";
//	welcomeMessage += FLAT_5 + " = [" + flat_5 + "] <br/>";
//	welcomeMessage += FLAT_10 + " = [" + flat_10 + "] <br/>";
//	welcomeMessage += MISHLAH_YAD + " = [" + mishlah_yad + "] <br/>";
//	welcomeMessage += TEL + " = [" + tel + "] <br/>";
}
